/**保存图片*/
const saveImage = (downLoadUrl) => {
	let that = this;
	// 向用户发起授权请求
	uni.authorize({
		scope: 'scope.writePhotosAlbum',
		success: () => {
			// 已授权
			downLoadImg(downLoadUrl);
		},
		fail: () => {
			// 拒绝授权，获取当前设置
			uni.getSetting({
				success: (result) => {
					if (!result.authSetting['scope.writePhotosAlbum']) {
						isAuth()
					}
				}
			});
		}
	})
};
/**
 * 下载资源，保存图片到系统相册
 */
export function downLoadImg(downLoadUrl) {
	uni.showLoading({
		title: '加载中'
	});
	uni.downloadFile({
		url: downLoadUrl,
		success: (res) => {
			uni.hideLoading();
			if (res.statusCode === 200) {
				uni.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function() {
						uni.showToast({
							title: "保存成功",
							icon: "none"
						});
					},
					fail: function() {
						uni.showToast({
							title: "保存失败",
							icon: "none"
						});
					}
				});
			}
		},
		fail: (err) => {
			console.log("保存图片授权失败", err);
			uni.showToast({
				title: "失败啦",
				icon: "none"
			});
		}
	})
};
/*
 * 引导用户开启权限
 */
export function isAuth() {
	uni.showModal({
		content: '由于您还没有允许保存图片到您相册里,无法进行保存,请点击确定允许授权',
		success: (res) => {
			if (res.confirm) {
				uni.openSetting({
					success: (result) => {
						console.log(result.authSetting);
					}
				});
			}
		}
	});
}
export default {
	saveImage
}